package com.mitgroup.dmrvs.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.mitgroup.dmrvs.R;
import com.mitgroup.dmrvs.utils.AppManager;
import com.mitgroup.dmrvs.utils.Utils;
import com.mitgroup.dmrvs.webservice.WSClient;

import org.json.JSONObject;
import org.json.JSONTokener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.trinea.android.common.util.PreferencesUtils;

public class MaintenanceItemManagerActivity extends AppCompatActivity {
    private static final int MSG_UPDATE_MAINTENANCE_ITEM = 1;
    private static final String BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_FAIL = "update_maintenance_item_fail";
    private static final String BUNDLE_LOCAL_ERROR = "local_error";
    private static final String BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_SUCCESS = "update_maintenance_item_success";
    private static final int MSG_GET_MAINTENANCE_ITEM= 2;
    private static final String BUNDLE_MSG_GET_MAINTENANCE_ITEM = "get_maintenance_item";
    private static final String BUNDLE_MSG_GET_MAINTENANCE_ITEM_SUCCESS= "get_maintenance_item_success";
    private static final String BUNDLE_MSG_GET_MAINTENANCE_ITEM_FAIL= "get_maintenance_item_fail";
    private Intent intent;
    private ProgressDialog progressDialog;
    /**handler对象*/
    private Handler handler;
    @BindView(R.id.btn_back)
    public Button btnBack;
    @OnClick(R.id.btn_back)
    public void onBack(View view) {
        this.finish();
    }
    @BindView(R.id.tv_user_name)
    public TextView tvUserName;
    @BindView(R.id.btn_nav01)
    public Button btnReceiveCar;
    @OnClick(R.id.btn_nav01)
    public void onReceiveCar(View view) {
        intent = new Intent(MaintenanceItemManagerActivity.this,
                ReceiveCarActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav02)
    public Button btnDiagnose;
    @OnClick(R.id.btn_nav02)
    public void onDiagnose(View view) {
        intent = new Intent(MaintenanceItemManagerActivity.this,
                CarDiagnoseActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.btn_nav03)
    public Button btnSettings;
    @OnClick(R.id.btn_nav03)
    public void onSettings(View view) {
        intent = new Intent(MaintenanceItemManagerActivity.this,
                SettingsActivity.class);
        startActivity(intent);
    }
    @BindView(R.id.iv_quit)
    public Button quit;
    @OnClick(R.id.iv_quit)
    public void onQuit(View view) {
        Utils.Exit(this);
    }

    @BindView(R.id.tv_maintenance_item_id_1)
    public TextView tvMaintenanceItemId1;
    @BindView(R.id.tv_item_name_1)
    public TextView tvItemName1;
    @BindView(R.id.et_man_hour_fee_1)
    public EditText etManHourFee1;
    @BindView(R.id.tv_spare_part_name_1)
    public TextView tvSpairPartName1;
    @BindView(R.id.tv_spare_part_number_1)
    public TextView tvSpairPartNumber1;
    @BindView(R.id.et_price_1)
    public EditText etPrice1;

    @BindView(R.id.tv_maintenance_item_id_2)
    public TextView tvMaintenanceItemId2;
    @BindView(R.id.tv_item_name_2)
    public TextView tvItemName2;
    @BindView(R.id.et_man_hour_fee_2)
    public EditText etManHourFee2;
    @BindView(R.id.tv_spare_part_name_2)
    public TextView tvSpairPartName2;
    @BindView(R.id.tv_spare_part_number_2)
    public TextView tvSpairPartNumber2;
    @BindView(R.id.et_price_2)
    public EditText etPrice2;

    @BindView(R.id.tv_maintenance_item_id_3)
    public TextView tvMaintenanceItemId3;
    @BindView(R.id.tv_item_name_3)
    public TextView tvItemName3;
    @BindView(R.id.et_man_hour_fee_3)
    public EditText etManHourFee3;
    @BindView(R.id.tv_spare_part_name_3)
    public TextView tvSpairPartName3;
    @BindView(R.id.tv_spare_part_number_3)
    public TextView tvSpairPartNumber3;
    @BindView(R.id.et_price_3)
    public EditText etPrice3;

    @BindView(R.id.tv_maintenance_item_id_4)
    public TextView tvMaintenanceItemId4;
    @BindView(R.id.tv_item_name_4)
    public TextView tvItemName4;
    @BindView(R.id.et_man_hour_fee_4)
    public EditText etManHourFee4;
    @BindView(R.id.tv_spare_part_name_4)
    public TextView tvSpairPartName4;
    @BindView(R.id.tv_spare_part_number_4)
    public TextView tvSpairPartNumber4;
    @BindView(R.id.et_price_4)
    public EditText etPrice4;

    @BindView(R.id.tv_maintenance_item_id_5)
    public TextView tvMaintenanceItemId5;
    @BindView(R.id.tv_item_name_5)
    public TextView tvItemName5;
    @BindView(R.id.et_man_hour_fee_5)
    public EditText etManHourFee5;
    @BindView(R.id.tv_spare_part_name_5)
    public TextView tvSpairPartName5;
    @BindView(R.id.tv_spare_part_number_5)
    public TextView tvSpairPartNumber5;
    @BindView(R.id.et_price_5)
    public EditText etPrice5;

    @BindView(R.id.tv_maintenance_item_id_6)
    public TextView tvMaintenanceItemId6;
    @BindView(R.id.tv_item_name_6)
    public TextView tvItemName6;
    @BindView(R.id.et_man_hour_fee_6)
    public EditText etManHourFee6;
    @BindView(R.id.tv_spare_part_name_6)
    public TextView tvSpairPartName6;
    @BindView(R.id.tv_spare_part_number_6)
    public TextView tvSpairPartNumber6;
    @BindView(R.id.et_price_6)
    public EditText etPrice6;

    @BindView(R.id.tv_maintenance_item_id_7)
    public TextView tvMaintenanceItemId7;
    @BindView(R.id.tv_item_name_7)
    public TextView tvItemName7;
    @BindView(R.id.et_man_hour_fee_7)
    public EditText etManHourFee7;
    @BindView(R.id.tv_spare_part_name_7)
    public TextView tvSpairPartName7;
    @BindView(R.id.tv_spare_part_number_7)
    public TextView tvSpairPartNumber7;
    @BindView(R.id.et_price_7)
    public EditText etPrice7;

    @BindView(R.id.tv_maintenance_item_id_8)
    public TextView tvMaintenanceItemId8;
    @BindView(R.id.tv_item_name_8)
    public TextView tvItemName8;
    @BindView(R.id.et_man_hour_fee_8)
    public EditText etManHourFee8;
    @BindView(R.id.tv_spare_part_name_8)
    public TextView tvSpairPartName8;
    @BindView(R.id.tv_spare_part_number_8)
    public TextView tvSpairPartNumber8;
    @BindView(R.id.et_price_8)
    public EditText etPrice8;

    @BindView(R.id.tv_maintenance_item_id_9)
    public TextView tvMaintenanceItemId9;
    @BindView(R.id.tv_item_name_9)
    public TextView tvItemName9;
    @BindView(R.id.et_man_hour_fee_9)
    public EditText etManHourFee9;
    @BindView(R.id.tv_spare_part_name_9)
    public TextView tvSpairPartName9;
    @BindView(R.id.tv_spare_part_number_9)
    public TextView tvSpairPartNumber9;
    @BindView(R.id.et_price_9)
    public EditText etPrice9;

    @BindView(R.id.tv_maintenance_item_id_10)
    public TextView tvMaintenanceItemId10;
    @BindView(R.id.tv_item_name_10)
    public TextView tvItemName10;
    @BindView(R.id.et_man_hour_fee_10)
    public EditText etManHourFee10;
    @BindView(R.id.tv_spare_part_name_10)
    public TextView tvSpairPartName10;
    @BindView(R.id.tv_spare_part_number_10)
    public TextView tvSpairPartNumber10;
    @BindView(R.id.et_price_10)
    public EditText etPrice10;

    @BindView(R.id.tv_maintenance_item_id_11)
    public TextView tvMaintenanceItemId11;
    @BindView(R.id.tv_item_name_11)
    public TextView tvItemName11;
    @BindView(R.id.et_man_hour_fee_11)
    public EditText etManHourFee11;
    @BindView(R.id.tv_spare_part_name_11)
    public TextView tvSpairPartName11;
    @BindView(R.id.tv_spare_part_number_11)
    public TextView tvSpairPartNumber11;
    @BindView(R.id.et_price_11)
    public EditText etPrice11;

    @BindView(R.id.tv_maintenance_item_id_12)
    public TextView tvMaintenanceItemId12;
    @BindView(R.id.tv_item_name_12)
    public TextView tvItemName12;
    @BindView(R.id.et_man_hour_fee_12)
    public EditText etManHourFee12;
    @BindView(R.id.tv_spare_part_name_12)
    public TextView tvSpairPartName12;
    @BindView(R.id.tv_spare_part_number_12)
    public TextView tvSpairPartNumber12;
    @BindView(R.id.et_price_12)
    public EditText etPrice12;

    @BindView(R.id.tv_maintenance_item_id_13)
    public TextView tvMaintenanceItemId13;
    @BindView(R.id.tv_item_name_13)
    public TextView tvItemName13;
    @BindView(R.id.et_man_hour_fee_13)
    public EditText etManHourFee13;
    @BindView(R.id.tv_spare_part_name_13)
    public TextView tvSpairPartName13;
    @BindView(R.id.tv_spare_part_number_13)
    public TextView tvSpairPartNumber13;
    @BindView(R.id.et_price_13)
    public EditText etPrice13;

    @BindView(R.id.tv_maintenance_item_id_14)
    public TextView tvMaintenanceItemId14;
    @BindView(R.id.tv_item_name_14)
    public TextView tvItemName14;
    @BindView(R.id.et_man_hour_fee_14)
    public EditText etManHourFee14;
    @BindView(R.id.tv_spare_part_name_14)
    public TextView tvSpairPartName14;
    @BindView(R.id.tv_spare_part_number_14)
    public TextView tvSpairPartNumber14;
    @BindView(R.id.et_price_14)
    public EditText etPrice14;

    @BindView(R.id.tv_maintenance_item_id_15)
    public TextView tvMaintenanceItemId15;
    @BindView(R.id.tv_item_name_15)
    public TextView tvItemName15;
    @BindView(R.id.et_man_hour_fee_15)
    public EditText etManHourFee15;
    @BindView(R.id.tv_spare_part_name_15)
    public TextView tvSpairPartName15;
    @BindView(R.id.tv_spare_part_number_15)
    public TextView tvSpairPartNumber15;
    @BindView(R.id.et_price_15)
    public EditText etPrice15;

    @BindView(R.id.tv_maintenance_item_id_16)
    public TextView tvMaintenanceItemId16;
    @BindView(R.id.tv_item_name_16)
    public TextView tvItemName16;
    @BindView(R.id.et_man_hour_fee_16)
    public EditText etManHourFee16;
    @BindView(R.id.tv_spare_part_name_16)
    public TextView tvSpairPartName16;
    @BindView(R.id.tv_spare_part_number_16)
    public TextView tvSpairPartNumber16;
    @BindView(R.id.et_price_16)
    public EditText etPrice16;
    @BindView(R.id.btn_upload_maintenance_item)
    public Button btnUploadMaintenanceItem;
    @OnClick(R.id.btn_upload_maintenance_item)
    public void onUploadMaintenanceItem(View view) {
        StringBuilder sb=new StringBuilder();
        sb.append("{\"maintenanceItems\":");
         sb.append("[");
        sb.append("{");
        String  maintenanceItemId=tvMaintenanceItemId1.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        String   manHourFee=etManHourFee1.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        String  sparePartName=tvSpairPartName1.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        String  sparePartNumber=tvSpairPartNumber1.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        String  price=etPrice1.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

       sb.append("{");
        maintenanceItemId=tvMaintenanceItemId2.getText().toString();
       sb.append("\"maintenanceItemId\":");
       sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee2.getText().toString();
       sb.append("\"manHourFee\":");
       sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName2.getText().toString();
       sb.append("\"sparePartName\":");
       sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber2.getText().toString();
       sb.append("\"sparePartNumber\":");
       sb.append("\""+sparePartNumber+"\",");
        price=etPrice2.getText().toString();
       sb.append("\"price\":");
       sb.append("\""+price+"\"");
       sb.append("},");

       sb.append("{");
        maintenanceItemId=tvMaintenanceItemId3.getText().toString();
       sb.append("\"maintenanceItemId\":");
       sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee3.getText().toString();
       sb.append("\"manHourFee\":");
       sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName3.getText().toString();
       sb.append("\"sparePartName\":");
       sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber3.getText().toString();
       sb.append("\"sparePartNumber\":");
       sb.append("\""+sparePartNumber+"\",");
        price=etPrice3.getText().toString();
       sb.append("\"price\":");
       sb.append("\""+price+"\"");
       sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId4.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee4.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName4.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber4.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice4.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId5.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee5.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName5.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber5.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice5.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId6.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee6.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName6.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber6.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice6.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId7.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee7.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName7.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber7.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice7.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId8.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee8.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName8.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber8.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice8.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId9.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee9.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName9.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber9.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice9.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId10.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee10.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName10.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber10.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice10.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId11.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee11.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName11.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber11.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice11.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId12.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee12.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName12.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber12.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice12.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId13.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee13.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName13.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber13.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice13.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId14.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee14.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName14.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber14.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice14.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId15.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee15.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName15.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber15.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice15.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("},");

        sb.append("{");
        maintenanceItemId=tvMaintenanceItemId16.getText().toString();
        sb.append("\"maintenanceItemId\":");
        sb.append("\""+maintenanceItemId+"\",");
        manHourFee=etManHourFee16.getText().toString();
        sb.append("\"manHourFee\":");
        sb.append("\""+manHourFee+"\",");
        sparePartName=tvSpairPartName16.getText().toString();
        sb.append("\"sparePartName\":");
        sb.append("\""+sparePartName+"\",");
        sparePartNumber=tvSpairPartNumber16.getText().toString();
        sb.append("\"sparePartNumber\":");
        sb.append("\""+sparePartNumber+"\",");
        price=etPrice16.getText().toString();
        sb.append("\"price\":");
        sb.append("\""+price+"\"");
        sb.append("}");
        sb.append("]}");
        updateMaintenanceItem(sb.toString());

    }
    private  String repairShopId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenance_item_manager);
        ButterKnife.bind(this);
        initView();

    }


    private void initView() {
        AppManager.getAppManager().addActivity(this);
        String userId = PreferencesUtils.getString(MaintenanceItemManagerActivity.this, "userId");
        String userName = PreferencesUtils.getString(MaintenanceItemManagerActivity.this, "userName");
        tvUserName.setText(userName);
        repairShopId = PreferencesUtils.getString(MaintenanceItemManagerActivity.this, "repairShopId");
        getMaintenanceItem(repairShopId);

    }

    private Handler myHandler = new Handler() {
        String key="";
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            String key="";
            switch (msg.what) {

                case MSG_GET_MAINTENANCE_ITEM:
                    key= msg.getData().keySet().iterator().next();
                    if (BUNDLE_MSG_GET_MAINTENANCE_ITEM_FAIL.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(MaintenanceItemManagerActivity.this,msg.getData().getString(BUNDLE_MSG_GET_MAINTENANCE_ITEM_FAIL), Toast.LENGTH_SHORT).show();
                    }
                    else if (BUNDLE_LOCAL_ERROR.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(MaintenanceItemManagerActivity.this,"服务器连接失败！", Toast.LENGTH_SHORT).show();

                    }

                    else if (BUNDLE_MSG_GET_MAINTENANCE_ITEM_SUCCESS.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        String content=msg.getData().getString(BUNDLE_MSG_GET_MAINTENANCE_ITEM_SUCCESS);
                        if(!"".equals(content))
                        {
                            try{
                                com.alibaba.fastjson.JSONObject obj = com.alibaba.fastjson.JSONObject.parseObject(content);
                                 JSONArray contentArray= obj.getJSONArray("content");
                                 String maintenanceItemId="";
                                 String itemName="";
                                  String manHourFee="";
                                  String sparePartName="";
                                  String sparePartNumber="";
                                  String price="";
                                 for(int i=0;i<contentArray.size();i++) {
                                    // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                                     com.alibaba.fastjson.JSONObject job = contentArray.getJSONObject(i);
                                     maintenanceItemId=job.getString("maintenanceItemId");
                                     itemName=job.getString("itemName");
                                     manHourFee=job.getString("manHourFee");
                                     sparePartName=job.getString("sparePartName");
                                     sparePartNumber=job.getString("sparePartNumber");
                                     price=job.getString("price");
                                     switch (i) {
                                         case  0:
                                            tvMaintenanceItemId1.setText(maintenanceItemId);
                                            tvItemName1.setText(itemName);
                                            etManHourFee1.setText(manHourFee);
                                            tvSpairPartName1.setText(sparePartName);
                                            tvSpairPartNumber1.setText(sparePartNumber);
                                            etPrice1.setText(price);
                                         break;
                                         case  1:
                                             tvMaintenanceItemId2.setText(maintenanceItemId);
                                             tvItemName2.setText(itemName);
                                             etManHourFee2.setText(manHourFee);
                                             tvSpairPartName2.setText(sparePartName);
                                             tvSpairPartNumber2.setText(sparePartNumber);
                                             etPrice2.setText(price);
                                             break;
                                         case  2:
                                             tvMaintenanceItemId3.setText(maintenanceItemId);
                                             tvItemName3.setText(itemName);
                                             etManHourFee3.setText(manHourFee);
                                             tvSpairPartName3.setText(sparePartName);
                                             tvSpairPartNumber3.setText(sparePartNumber);
                                             etPrice3.setText(price);
                                             break;
                                         case  3:
                                             tvMaintenanceItemId4.setText(maintenanceItemId);
                                             tvItemName4.setText(itemName);
                                             etManHourFee4.setText(manHourFee);
                                             tvSpairPartName4.setText(sparePartName);
                                             tvSpairPartNumber4.setText(sparePartNumber);
                                             etPrice4.setText(price);
                                             break;
                                         case  4:
                                             tvMaintenanceItemId5.setText(maintenanceItemId);
                                             tvItemName5.setText(itemName);
                                             etManHourFee5.setText(manHourFee);
                                             tvSpairPartName5.setText(sparePartName);
                                             tvSpairPartNumber5.setText(sparePartNumber);
                                             etPrice5.setText(price);
                                             break;
                                         case  5:
                                             tvMaintenanceItemId6.setText(maintenanceItemId);
                                             tvItemName6.setText(itemName);
                                             etManHourFee6.setText(manHourFee);
                                             tvSpairPartName6.setText(sparePartName);
                                             tvSpairPartNumber6.setText(sparePartNumber);
                                             etPrice6.setText(price);
                                             break;
                                         case  6:
                                             tvMaintenanceItemId7.setText(maintenanceItemId);
                                             tvItemName7.setText(itemName);
                                             etManHourFee7.setText(manHourFee);
                                             tvSpairPartName7.setText(sparePartName);
                                             tvSpairPartNumber7.setText(sparePartNumber);
                                             etPrice7.setText(price);
                                             break;
                                         case  7:
                                             tvMaintenanceItemId8.setText(maintenanceItemId);
                                             tvItemName8.setText(itemName);
                                             etManHourFee8.setText(manHourFee);
                                             tvSpairPartName8.setText(sparePartName);
                                             tvSpairPartNumber8.setText(sparePartNumber);
                                             etPrice8.setText(price);
                                             break;
                                         case  8:
                                             tvMaintenanceItemId9.setText(maintenanceItemId);
                                             tvItemName9.setText(itemName);
                                             etManHourFee9.setText(manHourFee);
                                             tvSpairPartName9.setText(sparePartName);
                                             tvSpairPartNumber9.setText(sparePartNumber);
                                             etPrice9.setText(price);
                                             break;
                                         case  9:
                                             tvMaintenanceItemId10.setText(maintenanceItemId);
                                             tvItemName10.setText(itemName);
                                             etManHourFee10.setText(manHourFee);
                                             tvSpairPartName10.setText(sparePartName);
                                             tvSpairPartNumber10.setText(sparePartNumber);
                                             etPrice10.setText(price);
                                             break;
                                         case  10:
                                             tvMaintenanceItemId11.setText(maintenanceItemId);
                                             tvItemName11.setText(itemName);
                                             etManHourFee11.setText(manHourFee);
                                             tvSpairPartName11.setText(sparePartName);
                                             tvSpairPartNumber11.setText(sparePartNumber);
                                             etPrice11.setText(price);
                                             break;
                                         case  11:
                                             tvMaintenanceItemId12.setText(maintenanceItemId);
                                             tvItemName12.setText(itemName);
                                             etManHourFee12.setText(manHourFee);
                                             tvSpairPartName12.setText(sparePartName);
                                             tvSpairPartNumber12.setText(sparePartNumber);
                                             etPrice12.setText(price);
                                             break;
                                         case  12:
                                             tvMaintenanceItemId13.setText(maintenanceItemId);
                                             tvItemName13.setText(itemName);
                                             etManHourFee13.setText(manHourFee);
                                             tvSpairPartName13.setText(sparePartName);
                                             tvSpairPartNumber13.setText(sparePartNumber);
                                             etPrice13.setText(price);
                                             break;
                                         case  13:
                                             tvMaintenanceItemId14.setText(maintenanceItemId);
                                             tvItemName14.setText(itemName);
                                             etManHourFee14.setText(manHourFee);
                                             tvSpairPartName14.setText(sparePartName);
                                             tvSpairPartNumber14.setText(sparePartNumber);
                                             etPrice14.setText(price);
                                             break;
                                         case  14:
                                             tvMaintenanceItemId15.setText(maintenanceItemId);
                                             tvItemName15.setText(itemName);
                                             etManHourFee15.setText(manHourFee);
                                             tvSpairPartName15.setText(sparePartName);
                                             tvSpairPartNumber15.setText(sparePartNumber);
                                             etPrice15.setText(price);
                                             break;
                                         case  15:
                                             tvMaintenanceItemId16.setText(maintenanceItemId);
                                             tvItemName16.setText(itemName);
                                             etManHourFee16.setText(manHourFee);
                                             tvSpairPartName16.setText(sparePartName);
                                             tvSpairPartNumber16.setText(sparePartNumber);
                                             etPrice16.setText(price);
                                             break;


                                     }
                                 }
                                }
                            catch (Exception e)
                            {
                                String error=e.getMessage();
                            }
                        }

                    }
                    break;
                case MSG_UPDATE_MAINTENANCE_ITEM:
                   key = msg.getData().keySet().iterator().next();
                    if (BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_FAIL.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(MaintenanceItemManagerActivity.this,msg.getData().getString(BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_FAIL), Toast.LENGTH_SHORT).show();
                    }
                    else if (BUNDLE_LOCAL_ERROR.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(MaintenanceItemManagerActivity.this,"服务器连接失败！", Toast.LENGTH_SHORT).show();

                    }

                    else if (BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_SUCCESS.equals(key)) {
                        if (progressDialog != null) {
                            progressDialog.dismiss();
                            progressDialog = null;
                        }
                        Toast.makeText(MaintenanceItemManagerActivity.this,msg.getData().getString(BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_SUCCESS), Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
            removeMessages(msg.what);
        }
    };

    protected void updateMaintenanceItem(String content)
    {

        if(Utils.isNetworkAvailable(MaintenanceItemManagerActivity.this)==false)
        {
            Toast.makeText(MaintenanceItemManagerActivity.this,"网络尚未连接，请连接网络！",Toast.LENGTH_SHORT).show();
            return;
        }

        final String []key={"content"};
        final String []value={content};
        progressDialog = ProgressDialog.show(MaintenanceItemManagerActivity.this,
                getResources().getString(R.string.notice), "正在设置保养项目...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bundle bundle = new Bundle();
                try {
                    String rs = WSClient.soapGetInfo("updateMaintenanceItem",key,value);
                    JSONTokener jsonTokener = new JSONTokener(rs);
                    JSONObject jsonObject = (JSONObject) jsonTokener
                            .nextValue();
                    String status = jsonObject.getString("status");
                    String content = jsonObject.get("content").toString();
                    if("1".equals(status))						{

                        bundle.putString(BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_SUCCESS,"保养项目设置成功！");
                    }
                    else
                    {
                        bundle.putString(BUNDLE_MSG_UPDATE_MAINTENANCE_ITEM_FAIL, "保养项目设置失败，请重试！");
                    }
                }
                catch (Exception e) {

                    bundle.putString(BUNDLE_LOCAL_ERROR, "访问服务器失败！");
                }
                Message msg = myHandler.obtainMessage();
                msg.what = MSG_UPDATE_MAINTENANCE_ITEM;
                msg.setData(bundle);
                myHandler.sendMessage(msg);
            }
        }).start();
    }
    protected void getMaintenanceItem(String repairShopId)
    {

        if(Utils.isNetworkAvailable(MaintenanceItemManagerActivity.this)==false)
        {
            Toast.makeText(MaintenanceItemManagerActivity.this,"网络尚未连接，请连接网络！",Toast.LENGTH_SHORT).show();
            return;
        }

        final String []key={"repairShopId"};
        final String []value={repairShopId};
        progressDialog = ProgressDialog.show(MaintenanceItemManagerActivity.this,
                getResources().getString(R.string.notice), "正在加载保养项目...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Bundle bundle = new Bundle();
                try {
                    String rs = WSClient.soapGetInfo("getMaintenanceItemByRepairShopId",key,value);
                    JSONTokener jsonTokener = new JSONTokener(rs);
                    JSONObject jsonObject = (JSONObject) jsonTokener
                            .nextValue();
                    String status = jsonObject.getString("status");
                    String content = jsonObject.get("content").toString();
                    if("1".equals(status))						{

                        bundle.putString(BUNDLE_MSG_GET_MAINTENANCE_ITEM_SUCCESS,rs);
                    }
                    else
                    {
                        bundle.putString(BUNDLE_MSG_GET_MAINTENANCE_ITEM_FAIL, "保养项目加载失败，请重试！");
                    }
                }
                catch (Exception e) {

                    bundle.putString(BUNDLE_LOCAL_ERROR, "访问服务器失败！");
                }
                Message msg = myHandler.obtainMessage();
                msg.what = MSG_GET_MAINTENANCE_ITEM;
                msg.setData(bundle);
                myHandler.sendMessage(msg);
            }
        }).start();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }
}
